package com.hex.liyue.launchdatemodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.glide.GlideCircleTransform;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.lookingdatemodule.bean.AppointmentDetailsResultBean;

import java.util.List;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterPerson extends RecyclerView.Adapter<AdapterPerson.MyHolder> {
    private List<AppointmentDetailsResultBean.DataBean.ApplyListBean> apply_list;
    private long invitations_time;//约会邀约时间
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onYesClickListener(String applyId, String acceptId);

        void onNoClickListener(String acceptId);

        void onLookGiftClickListener();

        void onChatClickListener(String fromUid, String fromName, String fromAvatar);

        void onItemClickListener(int position);
    }

    public AdapterPerson(List<AppointmentDetailsResultBean.DataBean.ApplyListBean> apply_list, long invitations_time) {
        this.apply_list = apply_list;
        this.invitations_time = invitations_time * 1000;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_launch_details, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        final AppointmentDetailsResultBean.DataBean.ApplyListBean applyListBean = apply_list.get(position);
        Glide.with(LiYueApplication.getInstance())
                .load(applyListBean.apply_user.avatar)
                .error(R.drawable.default_error_image)
                .transform(new GlideCircleTransform(LiYueApplication.getInstance()))
                .into(holder.iv_avater);
        holder.tv_name.setText(applyListBean.apply_user.nickname + " " + DateUtils.getAge(applyListBean.apply_user.birthday));
        holder.tv_state_1_info_1.setText("送您￥" + applyListBean.total_cash + "元礼物");
        holder.tv_state_4_info_2.setText("已收到Ta的" + applyListBean.total_cash + "元礼物");

        holder.ll_state_1.setVisibility(View.GONE);
        holder.ll_state_2.setVisibility(View.GONE);
        holder.ll_state_3.setVisibility(View.GONE);
        holder.ll_state_4.setVisibility(View.GONE);
        if (applyListBean.status.equals("3")) {
            //等待处理中
            holder.ll_state_1.setVisibility(View.VISIBLE);
        }
        if (applyListBean.status.equals("1")) {
            //接受
            holder.ll_state_2.setVisibility(View.VISIBLE);
        }
        if (applyListBean.status.equals("2")) {
            //拒绝
            holder.ll_state_3.setVisibility(View.VISIBLE);
        }
        if (applyListBean.status.equals("4")) {
            //完成约会
            holder.ll_state_4.setVisibility(View.VISIBLE);
        }
        holder.tv_operate_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onNoClickListener(applyListBean.accept_id);
                }
            }
        });
        holder.tv_operate_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onYesClickListener(applyListBean.apply_user.uid, applyListBean.accept_id);
                }
            }
        });
        holder.tv_look_gift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onLookGiftClickListener();
                }
            }
        });
        holder.tv_begin_chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onChatClickListener(applyListBean.apply_user.uid, applyListBean.apply_user.name, applyListBean.apply_user.avatar);
                }
            }
        });
        holder.iv_avater.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClickListener(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return apply_list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView iv_avater;
        TextView tv_name, tv_operate_no, tv_operate_yes, tv_begin_chat, tv_look_gift;
        TextView tv_state_1_info_1, tv_state_1_info_2;
        TextView tv_state_2_info_1, tv_state_2_info_2;
        TextView tv_state_3_info_1, tv_state_3_info_2;
        TextView tv_state_4_info_1, tv_state_4_info_2;

        LinearLayout ll_state_1, ll_state_2, ll_state_3, ll_state_4;

        public MyHolder(View itemView) {
            super(itemView);
            iv_avater = (ImageView) itemView.findViewById(R.id.iv_avater);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_state_1_info_1 = (TextView) itemView.findViewById(R.id.tv_state_1_info_1);
            tv_state_1_info_2 = (TextView) itemView.findViewById(R.id.tv_state_1_info_2);
            tv_state_2_info_1 = (TextView) itemView.findViewById(R.id.tv_state_2_info_1);
            tv_state_2_info_2 = (TextView) itemView.findViewById(R.id.tv_state_2_info_2);
            tv_state_3_info_1 = (TextView) itemView.findViewById(R.id.tv_state_3_info_1);
            tv_state_3_info_2 = (TextView) itemView.findViewById(R.id.tv_state_3_info_2);
            tv_state_4_info_1 = (TextView) itemView.findViewById(R.id.tv_state_4_info_1);
            tv_state_4_info_2 = (TextView) itemView.findViewById(R.id.tv_state_4_info_2);

            tv_operate_no = (TextView) itemView.findViewById(R.id.tv_operate_no);
            tv_operate_yes = (TextView) itemView.findViewById(R.id.tv_operate_yes);
            tv_begin_chat = (TextView) itemView.findViewById(R.id.tv_begin_chat);
            tv_look_gift = (TextView) itemView.findViewById(R.id.tv_look_gift);

            ll_state_1 = (LinearLayout) itemView.findViewById(R.id.ll_state_1);
            ll_state_2 = (LinearLayout) itemView.findViewById(R.id.ll_state_2);
            ll_state_3 = (LinearLayout) itemView.findViewById(R.id.ll_state_3);
            ll_state_4 = (LinearLayout) itemView.findViewById(R.id.ll_state_4);
        }

    }
}
